{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "1",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mIndexError\u001B[0m                                Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-9-483e2e97ccd1>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      8\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      9\u001B[0m \u001B[1;31m# this should be pyhealth, which is two level up from learning_models library\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 10\u001B[1;33m \u001B[0mroot_dir\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mPath\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mcurr_dir\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mparents\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;36m1\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     11\u001B[0m \u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mchdir\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mroot_dir\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     12\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32m~\\Anaconda3\\lib\\pathlib.py\u001B[0m in \u001B[0;36m__getitem__\u001B[1;34m(self, idx)\u001B[0m\n\u001B[0;32m    594\u001B[0m     \u001B[1;32mdef\u001B[0m \u001B[0m__getitem__\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0midx\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    595\u001B[0m         \u001B[1;32mif\u001B[0m \u001B[0midx\u001B[0m \u001B[1;33m<\u001B[0m \u001B[1;36m0\u001B[0m \u001B[1;32mor\u001B[0m \u001B[0midx\u001B[0m \u001B[1;33m>=\u001B[0m \u001B[0mlen\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 596\u001B[1;33m             \u001B[1;32mraise\u001B[0m \u001B[0mIndexError\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0midx\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    597\u001B[0m         return self._pathcls._from_parsed_parts(self._drv, self._root,\n\u001B[0;32m    598\u001B[0m                                                 self._parts[:-idx - 1])\n",
      "\u001B[1;31mIndexError\u001B[0m: 1"
     ]
    }
   ],
   "source": [
    "# environment setting\n",
    "import os\n",
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "# this should be learning_models\n",
    "curr_dir = os.getcwd()\n",
    "\n",
    "# this should be pyhealth, which is two level up from learning_models library\n",
    "root_dir = Path(curr_dir).parents[1]\n",
    "os.chdir(root_dir)\n",
    "\n",
    "sys.path.append(root_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from pyhealth.data.expdata_generator import ecgdata as expdata_generator\n",
    "expdata_id = '2020.0810.data.diagnose.ecg'\n",
    "# phenotyping\n",
    "cur_dataset = expdata_generator(expdata_id)\n",
    "# cur_dataset.get_exp_data(sel_task = 'diagnose', data_root = r'./datasets/ecg')\n",
    "cur_dataset.load_exp_data()\n",
    "cur_dataset.show_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyhealth.models.ecg.basicnn import BasicCNN as model\n",
    "#from pyhealth.models.ecg.rf import RandomForest as model\n",
    "#from pyhealth.models.ecg.xgboost import XGBoost as model\n",
    "\n",
    "\n",
    "expmodel_id = '2020.0821.ecg.diagnose.BasicCNN'\n",
    "clf = model(expmodel_id = expmodel_id, use_gpu = True)\n",
    "# clf.load_model('0')\n",
    "clf.fit(cur_dataset.train, cur_dataset.valid)\n",
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf.load_model()\n",
    "clf.inference(cur_dataset.test)\n",
    "results = clf.get_results()\n",
    "print (results)\n",
    "\n",
    "from pyhealth.evaluation.evaluator import func \n",
    "r = func(results['hat_y'], results['y'])\n",
    "print (r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}